package com.a01_2021.thousandstimes.instance.service.impl;

import com.a01_2021.thousandstimes.exception.CustomException;
import com.a01_2021.thousandstimes.exception.CustomExceptionType;
import com.a01_2021.thousandstimes.general.entity.Experience;
import com.a01_2021.thousandstimes.general.service.ExperienceService;
import com.a01_2021.thousandstimes.general.service.SysUserService;
import com.a01_2021.thousandstimes.instance.entity.Position;
import com.a01_2021.thousandstimes.instance.mapper.PositionMapper;
import com.a01_2021.thousandstimes.instance.service.IPositionService;
import com.a01_2021.thousandstimes.saas.service.PostService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author E1xP
 * @since 2021-03-10
 */
@Service
public class PositionServiceImpl extends ServiceImpl<PositionMapper, Position> implements IPositionService {

    @Resource
    PositionMapper positionMapper;

    @Resource
    PostService postService;

    @Resource
    SysUserService sysUserService;

    @Resource
    ExperienceService experienceService;

    /**
     * 添加职位
     *
     * @param position 职位实体
     * @return 职位实体类
     * @throws CustomException 当存在同名职位、不存在职位标签时抛出该异常
     */
    @Override
    public Position add(Position position) throws CustomException {
        if (this.isPosition(position.getName())) {
            throw new CustomException(CustomExceptionType.POSITION_HAD_BEEN_SET_ERROR, "该职位已存在");
        }
        if (!postService.isPost(position.getPostId())) {
            throw new CustomException(CustomExceptionType.POST_NOT_FOUND_ERROE, "不存在该职位标签");
        }
        positionMapper.insert(position);
        return position;
    }

    /**
     * 获取全部职位
     *
     * @return List 职位实体类列表
     */
    @Override
    public List<Position> getAll() {
        QueryWrapper<Position> queryWrapper = new QueryWrapper<>();
        return positionMapper.selectList(queryWrapper);
    }

    /**
     * 更换新职位
     *
     * @param userId     用户id
     * @param positionId 职位id
     * @param instanceId 实例id
     * @return Experience
     * @throws CustomException 当用户不存在、职位不存在时抛出该异常
     */
    public Experience changePosition(long userId, long positionId, long instanceId) throws CustomException {
        if (!sysUserService.isUser(userId)) {
            throw new CustomException(CustomExceptionType.USER_NOT_FOUND_ERROR, "找不到该用户");
        }
        Position position = positionMapper.selectById(positionId);
        if (position == null) {
            throw new CustomException(CustomExceptionType.POST_NOT_FOUND_ERROE, "找不到该职位");
        }
        return experienceService.changePosition(userId, position, instanceId);
    }

    /**
     * 用于检查该职位名称是否存在
     *
     * @param positionName 职位名称
     * @return 该职位是否存在
     */
    public boolean isPosition(String positionName) {
        QueryWrapper<Position> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("name", positionName);
        return positionMapper.selectCount(queryWrapper) > 0;
    }
}
